package com.rgst.controller;

import java.util.Optional;

import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rgst.comm.SearchParam;
import com.rgst.dao.entity.BusiConfig;
import com.rgst.dao.repository.BusiConfigRepository;
import com.rgst.dto.Result;
import com.rgst.utils.Gssert;
import com.rgst.utils.VoUtils;

/**
 * Created by qiao on 2017/5/11.
 */
@Controller
@RequestMapping("sysConfig")
public class SysConfigController {

    @Autowired
    private BusiConfigRepository busiConfigRepository;

    @RequestMapping("list")
    public String list() {
        return "rgst/sysConfig/list";
    }

    @RequestMapping("list-data")
    @ResponseBody
    public Page<BusiConfig> listData(SearchParam sp) {
        Sort sort = new Sort(Sort.Direction.ASC, "type", "sno");
        Pageable pageable = sp.getPageable(sort);
        ExampleMatcher matcher = ExampleMatcher.
                matching()
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.startsWith())
                .withIgnorePaths("id", "type", "value", "state","write","sno","remark");
        return busiConfigRepository.findAll(sp.getParam(BusiConfig.class,matcher), pageable);
    }

    @RequestMapping("form")
    public String form(@RequestParam(value = "id", required = false) Long id, Model model) {
        if (id != null) {
            model.addAttribute("info", busiConfigRepository.findById(id).get());
        } else {
            model.addAttribute("info", new BusiConfig());
        }
        return "rgst/sysConfig/form";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(BusiConfig busiConfig) {
        Assert.notNull(busiConfig, "参数错误");
        Assert.notNull(busiConfig.getId(), "参数错误");

        Optional<BusiConfig> oldConfig = busiConfigRepository.findById(busiConfig.getId());
        Assert.isTrue(oldConfig.isPresent(), "记录不存在");

        BusiConfig oldBusiConfig = oldConfig.get();
        VoUtils.mergeTo(busiConfig, oldBusiConfig);
        busiConfigRepository.save(oldBusiConfig);
        return Result.success("修改成功");
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam(value = "id") Long id) {
        busiConfigRepository.deleteById(id);
        return Result.success("处理成功");
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(BusiConfig sysConfig) {
        Gssert.notEmpty(sysConfig.getTitle(),"参数不能为空");
        Gssert.notEmpty(sysConfig.getValue(),"参数不能为空");
        busiConfigRepository.save(sysConfig);
        return Result.success("处理成功");
    }
}
